﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class Sys_AttachmentsDAO
    {
        /// <summary>
        /// 保存附件
        /// </summary>
        /// <param name="list">附件列表</param>
        /// <param name="bid">业务ID</param>
        /// <returns></returns>
        public int SaveAttachments(IList<Sys_Attachments> list, int bid)
        {
            List<string> sqls = new List<string>();
            foreach (var item in list)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into Sys_Attachments(");
                strSql.Append("FileName,FileUrl,FileDescription,ObjectName,ObjectNo,State)");
                strSql.Append(" values (");
                strSql.Append("'").Append(item.FileName).Append("',")
                    .Append("'").Append(item.FileUrl).Append("',")
                    .Append("'").Append(item.FileDescription).Append("',")
                    .Append("'").Append(item.ObjectName).Append("',")
                    .Append("").Append(bid).Append(",")
                    .Append("").Append(item.State).Append(")");
                sqls.Add(strSql.ToString());
            }
            return DbHelperSQL.ExecuteSqlTran(sqls);
        }
        /// <summary>
        /// 根据业务前缀+业务ID获取附件
        /// </summary>
        /// <param name="obj">数据源</param>
        /// <param name="id">业务ID</param>
        /// <returns></returns>
        public IList<TempModel> GetList(string obj, int id)
        {
            IList<TempModel> result = new List<TempModel>();
            string sql = "SELECT * FROM Sys_Attachments WHERE ObjectName=@ObjectName AND ObjectNo=@ObjectNo";
            SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@ObjectName",SqlDbType.NVarChar),
                new SqlParameter("@ObjectNo",SqlDbType.NVarChar)
            };
            param[0].Value = obj;
            param[1].Value = id;
            using (SqlDataReader sdr = DbHelperSQL.ExecuteReader(sql, param))
            {
                while (sdr.Read())
                {
                    TempModel ent = new TempModel();
                    ent = BuildObjectFromIDataReader(sdr);
                    result.Add(ent);
                }
                sdr.Close();
            }
            return result;
        }
        /// <summary>
        /// 从IDataReader中读取数据，组装成实体
        /// </summary>
        /// <param name="dr">数据读取接口</param>
        /// <returns>Sys_Attachments实体</returns>
        /// <remarks>
        /// </remarks>
        private TempModel BuildObjectFromIDataReader(IDataReader dr)
        {
            try
            {
                TempModel obj = new TempModel();
                obj.Id = (dr["Id"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("Id")));
                obj.FileName = (dr["FileName"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("FileName")));
                obj.FileUrl = (dr["FileUrl"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("FileUrl")));
                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception("从DataReader中读取数据失败，可能与相关的字段不存在有关！请检查！||" + ex.Message + "||" + ex.Source);
            }
        }
        /// <summary>
        /// 附件删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteSys_Attachments(int id)
        {
            string sql = "DELETE Sys_Attachments WHERE Id=@Id";
            SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@Id",SqlDbType.Int)
            };
            param[0].Value = id;
            return DbHelperSQL.ExecuteSql(sql, param);
        }
    }
}
